/*
 Copyright (C) 2010 Chris Hawkins. All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 * Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 
 * Neither the name of the author nor the names of its contributors may be used
 to endorse or promote products derived from this software without specific
 prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "TcUrlRequest.h"
#import "SvUrlRequest.h"

@implementation TASDTcUrlRequest
@synthesize lblMethod = lbl_Method, lblTimeout = lbl_Timeout, lblLength = lbl_Length, lblHeaderCount = lbl_HeaderCount;
@synthesize btnUrlDisclosure = btn_UrlDisclosure;

#pragma mark -- NSObject standard
- (id)                  initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString*) reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    }
    return self;
}
- (void)                dealloc {
    [btn_UrlDisclosure  release];
    [lbl_Method         release];
    [lbl_Timeout        release];
    [lbl_Length         release];
    [lbl_HeaderCount    release];
    [url_Request        release];
    [super dealloc];
}
#pragma mark -- ASDTTcBase overrides
- (NSDictionary*)       dictDisplayFromRequest: (ASDRqBase*) request_ {
    NSMutableDictionary *dict   = [[NSMutableDictionary alloc] initWithCapacity: 10];
    NSURLRequest        *urlReq = [request_ urlRequest];
    [dict setObject: [urlReq HTTPMethod]  forKey: @"HTTPMethod"];
    [dict addEntriesFromDictionary: [urlReq allHTTPHeaderFields]];
    return [dict autorelease];
}
- (NSString*)           title {
    return @"URL Request";
}
#pragma mark -- IBAction handers
- (IBAction)            btnUrlDisclosurePress {
    UIViewController    *viewController = [self parentViewController];
    if ( viewController != nil ) {
        if ( [viewController navigationController] != nil ) {
            TASDSvUrlRequest    *svUrlRequest = [[TASDSvUrlRequest alloc] initWithURLRequest: url_Request];
            [[viewController navigationController] pushViewController: svUrlRequest animated: YES];
            [svUrlRequest release];
        }
    }
}
#pragma mark -- ASDTTcUrlRequest specific
- (NSURLRequest*)       urlRequest {
    return url_Request;
}
- (void)                setUrlRequest: (NSURLRequest*) request_ {
    mASDreleaseNil( url_Request );
    url_Request = [request_ retain];
    BOOL isValid = ( url_Request != nil );
    btn_UrlDisclosure.userInteractionEnabled    = isValid;
    [[self btnUrlDisclosure] setUserInteractionEnabled: isValid];
    [[self accessoryView]    setUserInteractionEnabled: isValid];
    [[self lblMethod]        setText: ( !isValid ? @"?" : 
                                       [url_Request HTTPMethod] )];
    [[self lblTimeout]       setText: ( !isValid ? @"?" : 
                                       [NSString stringWithFormat: @"Timeout %.1f sec", [url_Request timeoutInterval]] )];
    if ( isValid ) {
        NSURL           *url        = [[url_Request URL] retain];
        NSString        *urlString  = [[url absoluteString] retain];
        NSDictionary    *headers    = [[url_Request allHTTPHeaderFields] retain];
        [[self lblLength]      setText: [NSString stringWithFormat: @"length %u",[urlString length]]];      
        [[self lblHeaderCount] setText: [NSString stringWithFormat: @"%u HTTP Headers",[headers count]]];      
        [urlString release];
        [url       release];
        [headers   release];
    } else {
        [[self lblLength]      setText: @"?"];        
        [[self lblHeaderCount] setText: @"?"];        
    }
    
}
+ (TASDTcUrlRequest*)   tableCell {
    TASDTcUrlRequest    *cell  = nil;
    NSArray             *aLoad = [[[NSBundle mainBundle] loadNibNamed: @"TcUrlRequest" owner: self options: nil] retain];
    if ( [aLoad count] == 1 ) {
        cell = [[aLoad objectAtIndex: 0] retain];
    }
    [aLoad release];
    return [cell autorelease];
}
@end
